/**
 * 
 */
package cn.edu.hstc.bean.utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import cn.edu.hstc.app.AppConfig;
import cn.edu.hstc.app.Constant;
import cn.edu.hstc.bean.Inform;

/**
 * @Description 通知公告帮助工具类
 * @author act262
 * @version 1.0
 * @since 2014-4-26 下午9:42:30
 * 
 */
public class InformUtils {
	/**
	 * 获取首页的公告通知列表
	 * 
	 * @param indexDocument
	 * @return
	 */
	public static List<Inform> getIndexInforms(Document indexDocument) {
		List<Inform> informsList = new ArrayList<Inform>();

		// 公告通知列表元素
		Elements liElements;

		// 匹配的selector 表达式
		String liInformRegex = "div.main div.main-r div.main-rm div.borde2 ul li";

		liElements = indexDocument.select(liInformRegex);

		Inform inform = null;
		Element urlElement;
		for (Element element : liElements) {
			urlElement = element.getElementsByTag("a").first();

			inform = new Inform();
			inform.setTitle(urlElement.text());
			inform.setUrl(urlElement.attr("abs:href"));
			inform.setPublishTime(element.getElementsByTag("span").first().text());

			informsList.add(inform);
			inform = null;
		}

		return informsList;
	}

	/**
	 * 获取到通知列表的通知内容
	 * 
	 * @return
	 */
	public static List<Inform> getInforms() {
		List<Inform> list = new ArrayList<Inform>();
		Document document = null;
		try {
			document = DocumentUtils.getWebDocument(Constant.INFORM_LIST_URL, AppConfig.CONNECT_TIME_OUT);
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 匹配通知内容的的选择器表达式
		String newsRegex = "table tbody tr td table tbody tr td.a26 ul li";
		// 获取到的通知列表元素
		Elements liElements = document.select(newsRegex);

		Inform inform = null;
		Element urlElement;
		for (Element element : liElements) {
			urlElement = element.getElementsByTag("a").first();
			inform = new Inform();
			inform.setTitle(urlElement.text());
			inform.setUrl(urlElement.attr("abs:href"));
			inform.setPublishTime(element.getElementsByTag("span").first().text());

			list.add(inform);
			urlElement = null;
		}

		return list;
	}

	/**
	 * 根据指定页面获取通知列表
	 * 
	 * @param index
	 *            页面索引值
	 * @return 如果指定的页面返回有数据则返回长度不为0;如果没有数据则返回长度为0
	 */
	public static List<Inform> getInformsByIndex(int index) {
		List<Inform> list = new ArrayList<Inform>();
		String url = Constant.INFORM_LIST_URL;
		if (index > 1) {
			url = url.substring(0, url.length() - 4) + "_" + index + ".asp";
		}

		Document document = null;
		try {
			document = DocumentUtils.getWebDocument(url, AppConfig.CONNECT_TIME_OUT);
		} catch (IOException e) {
			e.printStackTrace();
			return list;
		}

		// 匹配通知内容的的选择器表达式
		String newsRegex = "table tbody tr td table tbody tr td.a26 ul li";
		// 获取到的通知列表元素
		Elements liElements = document.select(newsRegex);

		Inform inform = null;
		Element urlElement;
		for (Element element : liElements) {
			urlElement = element.getElementsByTag("a").first();
			inform = new Inform();
			inform.setTitle(urlElement.text());
			inform.setUrl(urlElement.attr("abs:href"));
			inform.setPublishTime(element.getElementsByTag("span").first().text());

			list.add(inform);
			urlElement = null;
		}

		return list;
	}

}
